package com.aidex.biz.controller;

import java.util.List;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.PushRule;
import com.aidex.biz.service.PushRuleService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 分配规则Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/biz/bizPushRule")
public class PushRuleController extends BaseController {
    @Autowired
    private PushRuleService pushRuleService;

    /**
     * 查询分配规则列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(PushRule pushRule, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            pushRule.setCompanyId(null);
        } else {
            pushRule.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        pushRule.setPage(new PageDomain(request, response));
        return R.data(pushRuleService.findPage(pushRule));
    }

    /**
     * 获取分配规则详细信息
     */
    @GetMapping(value = "/{id}")
    public R<PushRule> detail(@PathVariable("id") String id) {
        return R.data(pushRuleService.get(id));
    }

    /**
     * 新增分配规则
     */
    @Log(title = "分配规则", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated PushRule pushRule) {
        return R.status(pushRuleService.save(pushRule));
    }

    /**
     * 修改分配规则
     */
    @Log(title = "分配规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated PushRule pushRule) {
        return R.status(pushRuleService.save(pushRule));
    }


    /**
     * 删除分配规则
     */
    @Log(title = "分配规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(pushRuleService.deletePushRuleByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(pushRuleService.findMaxSort(new PushRule()));
    }

    /**
     * 导出分配规则列表
     */
    @Log(title = "分配规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(PushRule pushRule) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            pushRule.setCompanyId(null);
        } else {
            pushRule.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<PushRule> list = pushRuleService.findList(pushRule);
        ExcelUtil<PushRule> util = new ExcelUtil<PushRule>(PushRule.class);
        return util.exportExcel(list, "分配规则数据");
    }

    /**
     * 获取分配规则详细信息
     */
    @GetMapping("/findExist")
    public R<PushRule> findExist(PushRule pushRule) {
        return R.data(pushRuleService.findExist(pushRule));
    }
}
















